let $ = layui.$;
/**
 * zmn 渠道-工单视频
 * @author yangshuangping
 * @since 2021-06-09 11:18:36
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'upload'], function (exports) {
        let bizType = [];
        let videoType = [];
        let form = layui.form;
        let layer = layui.layer;
        let upload = layui.upload;
        //初始化文件上传插件
        uploadVideo("video", "videoValue");
        // 提交
        form.render(null, "zmn-form");
        form.on('submit(zmn-form-submit)', function (obj) {

            // 获取业务类型和视频类型的复选框数据以”,“号分隔
            bizType = []
            $('#bizType input[type=checkbox]:checked').each(function () {
                bizType.push($(this).val());
            });

            videoType = []
            $('#videoType input[type=checkbox]:checked').each(function () {
                videoType.push($(this).val());
            });

            let data = obj.field;
            data.videoId = data.id;
            data.bizType = bizType.join(",")
            data.videoType = videoType.join(",")
            // 请选择业务类型
            if (data.bizType == "") {
                layer.msg("请选择业务类型！", {"icon": 2});
                return false;
            }
            // 视频类型不能为空
            if (data.videoType == "") {
                layer.msg("请选择类型！", {"icon": 2});
                return false;
            }

            let url = "/channel/workorder/video/addEdit.action";
            $.ajax({
                async: false,
                type: "POST", url: url,
                contentType: "application/json", dataType: "json", data: JSON.stringify(data),
                success: function (result) {
                    if (result.status == 200) {
                        parent.reload(result.message);
                        cancel();
                    } else {
                        layui.layer.msg(result.message, {"icon": 2});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            })
            return false;
        });

        /**
         * 上传视频
         * @param elemId dom元素
         * @param valueId 视频播放元素
         */
        let indexUploadVideo;

        function uploadVideo(elemId, valueId) {

            let data = layui.util.toDateString(new Date().getTime(), "yyyyMMdd");

            upload.render({
                elem: '#' + elemId,
                exts: 'avi|mp4',
                accept: 'video',
                data: {
                    folderName: 'channel/workorder/video/',
                    fileName: "channel_" + json.obj.videoId + "_" + data
                },
                url: '/common/uploadFile.action',
                size: 10 * 1024,
                before: function (obj) {
                    indexUploadVideo = layui.layer.load(1, {
                        shade: [0.5, '#DBDBDB']
                    });
                },
                done: function (res) {
                    if (res.code == 2) {
                        let data = res.data;
                        let style = "display:block !important; width: 100% !important; ;height: 130px !important;";
                        $("#" + valueId).val(data.src);
                        $("#channelVideoDisplay").attr("src", data.src);
                        $("#channelVideoDisplay").attr("style", style)
                        $("#channelUploadVideo").attr("style", "display:none !important")
                        json.obj.videoSrc = data.src
                    } else {
                        layer.msg("上传失败！")
                    }
                    layui.layer.close(indexUploadVideo);
                }
            });
        }

        //对外暴露的接口
        exports('channel_workorder_video_detail', {});
    }
);

/**
 * 取消关闭iframe
 */
function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}

/**
 * 删除视频
 * @param element dom元素
 */
function deleteVideo(element) {
    let style = "display:none !important;";
    $("#channelVideoDisplay").attr("src", '');
    $("#videoValue").val("");
    $("#channelVideoDisplay").attr("style", style)
    $("#channelUploadVideo").attr("style", "display:block !important")
}

/**
 * 预览视频
 * @param element
 */
function previewVideoDetail() {
    if (json.obj.videoSrc) {
        //调整预览大小
        let imgHtml = "<video src='" + json.obj.videoSrc + "' id='preview-channel' style='width:96%;height:90%;margin:2%;'";
        imgHtml += "autoplay=‘autoplay’  controls='controls'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [600 + 'px', 300 + 'px'],
            shadeClose: true,
            scrollbar: false,
            title: "", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
}